package com.anyi.util;

import com.anyi.entity.Article;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 安逸i
 * @version 1.0
 */
public class MyUtils {
    public static List<Article> tranFormView(Set<ZSetOperations.TypedTuple<String>> set, List<Article> list, String operate){
        HashMap<Long, Integer> longIntegerHashMap = new HashMap<>();
        set.forEach(item->{
            Long id = Long.valueOf(item.getValue());
            Integer num = item.getScore().intValue();
            longIntegerHashMap.put(id,num);
        });
        list.forEach(item->{
            Long id =Long.valueOf(item.getId());
            if(operate.equals("viewCounts")){
                item.setViewCounts(longIntegerHashMap.get(id));
            }else if(operate.equals("likeCounts")){
                item.setLikeCounts(longIntegerHashMap.get(id));
            }
        });
        return list;
    }
    public static Article tranFormOneView(Set<ZSetOperations.TypedTuple<String>> set, Article article, String operate){
        HashMap<Long, Integer> longIntegerHashMap = new HashMap<>();
        set.forEach(item->{
            Long id = Long.valueOf(item.getValue());
            Integer num = item.getScore().intValue();
            longIntegerHashMap.put(id,num);
        });
        Long id =Long.valueOf(article.getId());
        if(operate.equals("viewCounts")){
            article.setViewCounts(longIntegerHashMap.get(id));
        }else if(operate.equals("likeCounts")){
            article.setLikeCounts(longIntegerHashMap.get(id));
        }
        return article;
    }
}
